package com.oldbook.manage.service.impl;

import com.oldbook.common.utils.DateUtils;
import com.oldbook.common.utils.SecurityUtils;
import com.oldbook.manage.domain.CreditApply;
import com.oldbook.manage.mapper.CreditApplyMapper;
import com.oldbook.manage.service.ICreditApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学分申请Service业务层处理
 * 
 * @author Exiaoke
 * @date 2025-02-18
 */
@Service
public class CreditApplyServiceImpl implements ICreditApplyService 
{
    @Autowired(required = false)
    private CreditApplyMapper creditApplyMapper;

    /**
     * 查询学分申请
     * 
     * @param applyId 学分申请主键
     * @return 学分申请
     */
    @Override
    public CreditApply selectCreditApplyByApplyId(Long applyId)
    {
        return creditApplyMapper.selectCreditApplyByApplyId(applyId);
    }

    /**
     * 查询学分申请列表
     * 
     * @param creditApply 学分申请
     * @return 学分申请
     */
    @Override
    public List<CreditApply> selectCreditApplyList(CreditApply creditApply)
    {
        return creditApplyMapper.selectCreditApplyList(creditApply);
    }

    /**
     * 新增学分申请
     * 
     * @param creditApply 学分申请
     * @return 结果
     */
    @Override
    public int insertCreditApply(CreditApply creditApply)
    {
        creditApply.setCreateTime(DateUtils.getNowDate());
        //student获取当前登录用户id
        creditApply.setStudentId(SecurityUtils.getLoginUser().getUser().getStudentId());
        //创建人
        creditApply.setCreateBy(SecurityUtils.getUsername());
        //提交日期获取当前时间
        creditApply.setSubmitDate(DateUtils.getNowDate());
        //审核状态设置为待审核
        creditApply.setStatus("0");
        //审核人设置为admin
        creditApply.setAuditBy("admin");
        return creditApplyMapper.insertCreditApply(creditApply);
    }

    /**
     * 修改学分申请
     * 
     * @param creditApply 学分申请
     * @return 结果
     */
    @Override
    public int updateCreditApply(CreditApply creditApply)
    {
        creditApply.setUpdateTime(DateUtils.getNowDate());
        //积分兑换学分：100积分兑换1学分
        creditApply.setCreditEarned((creditApply.getCreditEarned()));
        return creditApplyMapper.updateCreditApply(creditApply);
    }

    /**
     * 批量删除学分申请
     * 
     * @param applyIds 需要删除的学分申请主键
     * @return 结果
     */
    @Override
    public int deleteCreditApplyByApplyIds(Long[] applyIds)
    {
        return creditApplyMapper.deleteCreditApplyByApplyIds(applyIds);
    }

    /**
     * 删除学分申请信息
     * 
     * @param applyId 学分申请主键
     * @return 结果
     */
    @Override
    public int deleteCreditApplyByApplyId(Long applyId)
    {
        return creditApplyMapper.deleteCreditApplyByApplyId(applyId);
    }

    /**
     * 根据学号查询学分申请
     */
    @Override
    public List<CreditApply> selectCreditApplyByStudentId(String studentId){
        return creditApplyMapper.selectCreditApplyByStudentId(studentId);
    }
}
